package com.wu.common.utils;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class HttpUtils {
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    public HttpUtils() {
    }

    public static String doPostJson(String url, String jsonStr) {
        String result = null;
        HttpPost post = new HttpPost(url);

        try {
            CloseableHttpClient client = HttpClients.createDefault();
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
            post.setConfig(requestConfig);
            post.addHeader("content-type", "application/json");
            StringEntity myEntity = new StringEntity(jsonStr, "UTF-8");
            post.setEntity(myEntity);
            HttpResponse response = client.execute(post);
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                String respBody = new String(EntityUtils.toString(resEntity));

                try {
                    result = respBody;
                } catch (Exception var15) {
                    var15.printStackTrace();
                }
            }
        } catch (IOException var16) {
            var16.printStackTrace();
        } finally {
            post.releaseConnection();
        }

        return result;
    }

    public void ssl(String url) {
        CloseableHttpClient httpclient = null;

        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream instream = new FileInputStream(new File("d:\\tomcat.keystore"));

            try {
                trustStore.load(instream, "123456".toCharArray());
            } catch (CertificateException var61) {
                var61.printStackTrace();
            } finally {
                try {
                    instream.close();
                } catch (Exception var59) {
                    var59.printStackTrace();
                }

            }

            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, (String[])null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            HttpGet httpget = new HttpGet(url);
            log.debug("executing request" + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);

            try {
                HttpEntity entity = response.getEntity();
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    log.debug("Response content length: " + entity.getContentLength());
                    log.debug(EntityUtils.toString(entity));
                    EntityUtils.consume(entity);
                }
            } finally {
                response.close();
            }
        } catch (ParseException var63) {
            var63.printStackTrace();
        } catch (IOException var64) {
            var64.printStackTrace();
        } catch (KeyManagementException var65) {
            var65.printStackTrace();
        } catch (NoSuchAlgorithmException var66) {
            var66.printStackTrace();
        } catch (KeyStoreException var67) {
            var67.printStackTrace();
        } finally {
            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException var58) {
                    var58.printStackTrace();
                }
            }

        }

    }

    public static String postForm(String url, List<NameValuePair> formparams) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build();
        httppost.setConfig(requestConfig);

        String var9;
        try {
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            log.debug("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);

            try {
                HttpEntity entity = response.getEntity();
                if (entity == null) {
                    return null;
                }

                String result = EntityUtils.toString(entity, "UTF-8");
                log.debug("--------------------------------------");
                log.debug("Response content: " + result);
                log.debug("--------------------------------------");
                var9 = result;
            } finally {
                response.close();
            }
        } catch (ClientProtocolException var33) {
            var33.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException var34) {
            var34.printStackTrace();
            return null;
        } catch (IOException var35) {
            var35.printStackTrace();
            return null;
        } finally {
            try {
                httpclient.close();
            } catch (IOException var31) {
                var31.printStackTrace();
            }

        }

        return var9;
    }

    public static String post(String url, HashMap<String, String> map) {
        String result = "";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build();
        httppost.setConfig(requestConfig);
        List<NameValuePair> formparams = new ArrayList();
        Iterator it = map.keySet().iterator();

        while(it.hasNext()) {
            Object key = it.next();
            formparams.add(new BasicNameValuePair(key.toString(), (String)map.get(key)));
        }

        try {
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            CloseableHttpResponse response = httpclient.execute(httppost);

            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                    log.info("Response content: " + result);
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException var32) {
            var32.printStackTrace();
        } catch (UnsupportedEncodingException var33) {
            var33.printStackTrace();
        } catch (IOException var34) {
            var34.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException var30) {
                var30.printStackTrace();
            }

        }

        return result;
    }

    public static String get(String url) {
        String result = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();

        try {
            HttpGet httpget = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build();
            httpget.setConfig(requestConfig);
            log.debug("executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);

            try {
                HttpEntity entity = response.getEntity();
                System.out.println("--------------------------------------");
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                    log.info("Response content: " + result);
                }

                System.out.println("------------------------------------");
            } finally {
                response.close();
            }
        } catch (ClientProtocolException var28) {
            var28.printStackTrace();
        } catch (ParseException var29) {
            var29.printStackTrace();
        } catch (IOException var30) {
            var30.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException var26) {
                var26.printStackTrace();
            }

        }

        return result;
    }

    public static String dogetJson(String urlString) {
        StringBuilder sb = new StringBuilder();

        try {
            URL url = new URL(urlString);
            HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection();
            httpUrlConnection.setRequestMethod("GET");
            httpUrlConnection.setConnectTimeout(30000);
            httpUrlConnection.setReadTimeout(30000);
            httpUrlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            httpUrlConnection.connect();
            InputStream inStream = httpUrlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inStream));

            String line;
            while((line = br.readLine()) != null) {
                sb.append(line);
            }

            br.close();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        return sb.toString();
    }

    private static String getStringTime() {
        SimpleDateFormat sy = new SimpleDateFormat("yyyy");
        SimpleDateFormat sm = new SimpleDateFormat("MM");
        SimpleDateFormat sd = new SimpleDateFormat("dd");
        SimpleDateFormat sh = new SimpleDateFormat("HH");
        SimpleDateFormat smin = new SimpleDateFormat("mm");
        SimpleDateFormat ss = new SimpleDateFormat("ss");
        Timestamp now = new Timestamp(System.currentTimeMillis());
        String dateStr = sy.format(now) + sm.format(now) + sd.format(now) + sh.format(now) + smin.format(now) + ss.format(now);
        return dateStr;
    }
}

